package com.hddf.project.upload_cache;

import java.util.List;
import java.util.Set;

import org.bson.types.ObjectId;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.QueryBuilder;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

public class CacheDaoImpl_MongoDB {
	
	public DBObject getObjById(String id, DBCollection c) {
		ObjectId oid = new ObjectId(id);
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("_id").is(oid).get();
		DBObject entity = c.findOne(query);
		return entity;
	}
	
	public String save(DBObject obj, DBCollection coll) {
		WriteResult wr = coll.save(obj, WriteConcern.SAFE);
		int rs = wr.getN();
		String s = ((ObjectId)obj.get("_id")).toString();
		if(rs==0)
			return s;
		else
			return null;
	}
	
	public boolean delete(String id, DBCollection coll) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		ObjectId oid = new ObjectId(id);
		DBObject query = queryBuilder.put("_id").is(oid).get();
		DBObject obj = coll.findAndRemove(query);//返回被执行删除的对象,没执行删除返回null
		if(null!=obj){
			return true;
		}else{
			return false;
		}
		/*WriteResult wr = coll.remove(query, WriteConcern.NORMAL);
		int rs = wr.getN(); //返回执行的条数，如果返回0 那么是没有执行操作，如果删除了2条 那么返回2
		System.out.println(rs);
		return true;*/
	}
	
	public int deleteByUserId(int uid, DBCollection coll) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(uid).get();
		WriteResult wr = coll.remove(query, WriteConcern.NORMAL);
		int rs = wr.getN(); //返回执行的条数，如果返回0 那么是没有执行操作，如果删除了2条 那么返回2
		return rs;
	}

	public int deleteByUserIdSet(Set<Integer> set, DBCollection coll) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").in(set).get();
		WriteResult wr = coll.remove(query, WriteConcern.NORMAL);
		int rs = wr.getN(); //返回执行的条数，如果返回0 那么是没有执行操作，如果删除了2条 那么返回2
		return rs;
	}
	
	public DBCursor getCollCursor(DBCollection c) {
		return  c.find();
	}
	
	public DBObject getUserWebtoken_lifetime(int uid,DBCollection c){
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(uid).get();
		BasicDBObject fields = new BasicDBObject();
		fields.append("webtoken_lifetime",1).append("_id", 0);
		DBObject entity = c.findOne(query,fields);
		return entity;
	}
	
	public List<DBObject> getObjList(DBCollection c) {
		List<DBObject> list = c.find().toArray();
		return list;
	}
	
	public int getAuthTotalCount(int state, DBCollection c){
		int totalCount = c.find().length();
		return totalCount;
	}

	

	public boolean update(DBObject obj, DBCollection c) {
		String id = (String)obj.get("_id");
		ObjectId oid = new ObjectId(id);
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("_id").is(oid).get();
		c.setWriteConcern(WriteConcern.NORMAL);
		//要将 _id 删除 obj对象包含 _id 不能更新
		obj.removeField("_id");
		//WriteResult wr = c.update(query, obj);
		WriteResult wr = c.update(query, obj, false, false);
		int rs = wr.getN(); 
		if(rs==1)
			return true;
		else
			return false;
	}
	
	public List<DBObject> getAuthByIdArray(int[] array, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		//DBObject query = queryBuilder.put("user_id").is(userid).get();
		DBObject query = queryBuilder.put("auth_id").in(array).get();
		List<DBObject> list = c.find(query).toArray();
		//int[] arr = {1,7};
		//String[] arr = {"修改密码","用户管理3"};
		/*List<DBObject> list2 = c.find(new BasicDBObject("auth_id", 
				new BasicDBObject(QueryOperators.IN, array))).toArray();*/
		return list;
	}
	

}
